package cn.net.autocode.platform.auth;

import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.constants.PlatformConstant;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;

@Controller
@RequestMapping
public class PlatformRootUri {

    private final Dao dao;

    public PlatformRootUri(Dao dao) {
        this.dao = dao;
    }

    @RequestMapping("/")
    public void showDefaultView(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JSONObject platform = dao.queryJSON("select * from autocode_platform");
        String defaultView = platform.getString("defaultView")==null?"":platform.getString("defaultView");
        defaultView = defaultView.trim();
        if (defaultView.isBlank() || defaultView.equals("/") ||defaultView.equals("\\")) {
            resp.sendRedirect(req.getContextPath() + PlatformConstant.MAIN_PATH);
        }else{
            if (defaultView.startsWith("/")){
                resp.sendRedirect(req.getContextPath() + defaultView);
            }else{
                resp.sendRedirect(req.getContextPath() + "/" +defaultView);
            }
        }

    }
}
